package com.linux.image;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;

/**
 * Created by huhx on 2017-07-15.
 */
public class ImageTest {

    @Test
    public void readImage_1() {
        File file = new File("D:/liuling.png");
        try {
            InputStream stream = new FileInputStream(file);
            byte[] bytes = new byte[stream.available()];
            stream.read(bytes);
            System.out.println(new String(bytes));
            stream.close();
            String string = Base64.encodeBase64String(bytes);
            System.out.println(string);

//            stream.read(bytes);
//            System.out.println(new String(bytes));
//            System.out.println(Arrays.toString(bytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void stringGetBytes() {
        String string = "我是huhx";
        byte[] bytes = string.getBytes();
        System.out.println(Arrays.toString(bytes));
        System.out.println(new String(bytes));
    }

    @Test
    public void stringToImage() {
        File file = new File("C:\\Users\\76801\\Desktop/liuling.txt");
        try {
            String string = FileUtils.readFileToString(file);
            byte[] bytes = Base64.decodeBase64(string);

            FileOutputStream fileOutputStream = new FileOutputStream("D:/liuling.png");
            IOUtils.write(bytes, fileOutputStream);
            fileOutputStream.close();

            System.out.println(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
